package com.stuManSystem.views;

import com.stuManSystem.base.BaseDao;
import com.stuManSystem.config.AppConstants;
import com.stuManSystem.config.Dao;
import com.stuManSystem.dao.StudentDao;
import com.stuManSystem.model.Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//更新
public class UpdateView extends JFrame {


    private JPanel jPanelCenter, jPanelSouth;
    private JButton updateButton,exitButton;
    private JTextField name,sno,deparment,hometown,mark,email,tel,sex;


    public UpdateView(){
        init();
    }
    public void init(){
        setTitle(AppConstants.UPDATEVIEW_TITE);
        jPanelCenter = new JPanel();
        jPanelCenter.setLayout(new GridLayout(9,2));

        jPanelCenter.add(new JLabel(AppConstants.STUDENT_NAME));
        name = new JTextField();
        jPanelCenter.add(name);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_SNO));
        sno =new JTextField();
        jPanelCenter.add(sno);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_SEX));
        sex=new JTextField();
        jPanelCenter.add(sex);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_DEPARTMETN));
        deparment=new JTextField();
        jPanelCenter.add(deparment);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_HOMETOWN));
        hometown=new JTextField();
        jPanelCenter.add(hometown);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_MARK));
        mark=new JTextField();
        jPanelCenter.add(mark);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_EMAIL));
        email=new JTextField();
        jPanelCenter.add(email);
        jPanelCenter.add(new JLabel(AppConstants.STUDENT_TEL));
        tel=new JTextField();
        jPanelCenter.add(tel);


        jPanelCenter.add(new JLabel("--------------------------------------------"));
        jPanelCenter.add(new JLabel("--------------------------------------------"));
        jPanelSouth = new JPanel(new GridLayout(1,2));
        updateButton = new JButton(AppConstants.UPDATEVIEW_UPDATEBUTTON);
        exitButton = new JButton(AppConstants.EXITBUTTON);

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Student stu=new Student();
                stu.setDepartment(deparment.getText());
                stu.setName( name.getText());
                stu.setSno(sno.getText());
                stu.setSex(sex.getText());
                stu.setHomeTown(hometown.getText());
                stu.setMark(mark.getText());
                stu.setEmail(email.getText());
                stu.setTel(tel.getText());
                ((StudentDao) BaseDao.getAbilityDAO(Dao.StudentDao)).update(stu);
                new MainView();
                dispose();

            }
        });
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("退出");
                dispose();
            }
        });

        jPanelSouth.add(updateButton);
        jPanelSouth.add(exitButton);

        this.add(jPanelCenter,BorderLayout.CENTER);
        this.add(jPanelSouth,BorderLayout.SOUTH);

        //设置出现的位置和大小
        this.setBounds(250, 350, 375, 340);
        //窗体对象，默认是不可见的。通过设置setVisible，使其可见。
        this.setVisible(true);
        //拒绝改变窗体大小
        this.setResizable(false);
        //设置关闭窗体时的运行效果，DISPOSE_ON_CLOSE：关闭
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {

        new UpdateView();
    }
}
